const app = getApp()
//获得全局js变量
var util = require('../../utils/util.js');
Page({
  data: {
    navIndex: 1,
    scenicBoxTop: 0,
    scrollTop: 0,
    glList: [
      { id: 1, order: '订单管理', allOrder: '查看全部订单' },
      { id: 2, order: '库存管理', allOrder: '全部库存' },
      { id: 3, order: '补贴管理', allOrder: '补贴库存' }
    ],
    loadingType: 0,
    contentText: {
      contentdown: "上拉显示更多",
      contentrefresh: "正在加载...",
      contentnomore: "没有更多数据了"
    },
    id: 0,
    userinfo: [],
    info: [],
    longitude: 0,
    latitude: 0,
    shoplist: [],
    shoplist1: []
  },

  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    });
  },

  onLoad(options) {
    this.setData({
      userinfo: wx.getStorageSync('user'),
      id: options.id,
      longitude: wx.getStorageSync('userlon'),
      latitude: wx.getStorageSync('userlat')
    });

    if (!this.data.longitude || !this.data.latitude) {
      this.choose_location();
    } else {
      this.getinfo();
      this.getshoplist1();
    }
  },

  onShow() {},

  navtab(n) {
    this.setData({
      navIndex: n,
      p: 1,
      shoplist1: [],
      shoplist: []
    });

    if (n == 0) {
      this.getshoplist();
    } else if (n == 1) {
      this.getshoplist1();
    }
  },

  copynum(e) {
    const str = e.currentTarget.dataset.info;
    wx.setClipboardData({
      data: str,
      success: function () {
        wx.showToast({
          title: '复制成功',
          icon: 'none'
        });
      }
    });
  },

  choose_location() {
    wx.showLoading({
      title: '定位中...'
    });

    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        wx.hideLoading();
        const latitude = res.latitude;
        const longitude = res.longitude;
        wx.setStorageSync('userlon', longitude);
        wx.setStorageSync('userlat', latitude);
        this.setData({
          longitude: longitude,
          latitude: latitude
        });
        this.getinfo();
        this.getshoplist1();
      }
    });
  },

  getshoplist() {
    const that = this;
    util.httpRrquest(app.data.apiurl + 'shop/getChildshop', 'GET',{id: that.data.id,
        userlon: that.data.longitude,
        userlat: that.data.latitude})
    .then(function (response) {
        var data = response.data.data;
        that.setData({
          shoplist: data.data
        });
    }, function (error) {
        console.log(error);
    });
    
  },

  getshoplist1() {
    const that = this;
    util.httpRrquest(app.data.apiurl + 'shop/getMyShopChildShop', 'GET',{id: that.data.id,
        userlon: that.data.longitude,
        userlat: that.data.latitude})
    .then(function (response) {
      var data = response.data.data;
        if (data.has == 1) {
          that.setData({
            shoplist1: data.data
          });
        }
    }, function (error) {
        console.log(error);
    });
  },

  getinfo() {
    const that = this;
    util.httpRrquest(app.data.apiurl + 'shop/getMyshop', 'GET',{})
    .then(function (response) {
      console.log(response.data);
      var data = response.data.data;
        if (data.has == 1) {
          that.setData({
            info: data.data
          });
        }
    }, function (error) {
        console.log(error);
    });
  },

  test(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/shopDetail/shopDetail?id=' + id
    });
  },

  toDetail(e) {
    const index = e.currentTarget.dataset.index;
    const id = this.data.id;
    if (index == 0) {
      wx.navigateTo({
        url: '/pages/orderList/orderList?id=' + id
      });
    } else if (index == 1) {
      wx.navigateTo({
        url: '/pages/kuCunGuanLi/kuCunGuanLi?id=' + id
      });
    } else if (index == 2) {
      wx.navigateTo({
        url: '/pages/buTieGuanLi/buTieGuanLi?id=' + id
      });
    } else if (index == 3) {
      wx.navigateTo({
        url: '/pages/zitiGuanLi/zitiGuanLi?id=' + id
      });
    }
  },

  jump() {
    wx.navigateTo({
      url: '/pages/commissionList/commissionList?id=' + this.data.id + '&type=1'
    });
  }
});